package com.hyphenate.easeui.widget.chatrow;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMMessage.ChatType;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.R;
import com.hyphenate.easeui.utils.EaseUserUtils;
import com.hyphenate.exceptions.HyphenateException;
import com.paishen.common.util.BaseUtil;

public class EaseChatRowGift extends EaseChatRow {

    private TextView tvOtherNick;
    private ImageView ivGift;

    private String giftLollipop, giftCake, giftWine;

    private String selfNick, otherNick;

    public EaseChatRowGift(Context context, EMMessage message,String selfNick, String otherNick, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
        giftLollipop = context.getString(R.string.gift_lollipop);
        giftCake = context.getString(R.string.gift_cake);
        giftWine = context.getString(R.string.gift_wine);

        this.selfNick = selfNick;
        this.otherNick = otherNick;
    }

    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_gift : R.layout.ease_row_sent_gift, this);
    }

    @Override
    protected void onFindViewById() {
        tvOtherNick = (TextView) findViewById(R.id.tv_othername);
        ivGift = (ImageView) findViewById(R.id.iv_gift);
    }

    @Override
    public void onSetUpView() {
        try {
            String mName = message.getStringAttribute(EaseConstant.PARAMS_NAME);
            String mValue = message.getStringAttribute(EaseConstant.PARAMS_VALUE);
            ivGift.setVisibility(View.VISIBLE);
            if (TextUtils.equals(mName, giftLollipop)) {
                Glide.with(context).load(R.drawable.ease_chat_gift_2_s).into(ivGift);
            } else if (TextUtils.equals(mName, giftCake)) {
                Glide.with(context).load(R.drawable.ease_chat_gift_5_s).into(ivGift);
            } else if (TextUtils.equals(mName, giftWine)) {
                Glide.with(context).load(R.drawable.ease_chat_gift_10_s).into(ivGift);
            } else {
                ivGift.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            BaseUtil.showToast("e:" + e);
            e.printStackTrace();
        }
        // tvOtherNick.setText(EaseUserUtils.getUserInfo(message.direct() == EMMessage.Direct.RECEIVE ?  message.getFrom(): message.getTo()).getNick());
        tvOtherNick.setText(otherNick);
        userAvatarView.setVisibility(View.GONE);

        handleTextMessage();
    }

    protected void handleTextMessage() {
        if (message.direct() == EMMessage.Direct.SEND) {
            setMessageSendCallback();
            switch (message.status()) {
                case CREATE:
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    // 发送消息
//                sendMsgInBackground(message);
                    break;
                case SUCCESS: // 发送成功
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.GONE);
                    break;
                case FAIL: // 发送失败
                    progressBar.setVisibility(View.GONE);
                    statusView.setVisibility(View.VISIBLE);
                    break;
                case INPROGRESS: // 发送中
                    progressBar.setVisibility(View.VISIBLE);
                    statusView.setVisibility(View.GONE);
                    break;
                default:
                    break;
            }
        } else {
            if (!message.isAcked() && message.getChatType() == ChatType.Chat) {
                try {
                    EMClient.getInstance().chatManager().ackMessageRead(message.getFrom(), message.getMsgId());
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
    }


}
